package com.study.threadpool.service;

import org.springframework.stereotype.Service;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author flwu
 * @date 2020/3/11 7:08 PM
 * @MOTTO 保持空和敬畏
 */
@Service
public class SynchronizedService {

    private int count = 0;
    private volatile Integer cc = 0;
    private StringBuilder names;

    public int getCount() {
        return this.count;
    }

    /**
     * 同步块修饰方法
     */
    public  void test1(CountDownLatch countDownLatch) {
        for(int i=0; i<10000; i++) {
            this.count++;
        }
        countDownLatch.countDown();
    }

    /**
     * 同步块修饰静态方法
     */
    public static synchronized void test2() {

    }

    /**
     * 同步方法快测试
     * @param countDownLatch
     */
    public void test3(CountDownLatch countDownLatch) {
        synchronized(this) {
            for(int i=0; i<100; i++) {
                try{
                    Thread.sleep(1);
                } catch (Exception e) {

                }
                count++;
            }
        }
        countDownLatch.countDown();
    }

}
